<?php
declare(strict_types = 1);

namespace Tool;
/**
 * ajax 组装数据
 * @author Administrator
 *
 */
class AjaxReturn
{
    public static function sendData($data, string $message = '成功') :array
    {
        $result =  [
            'status' => 1,
            'message' => $message
        ];

        if (!isset($data['cmd'])) {
            $result['data'] = $data;
            return $result;
        }

        unset($data['cmd']);

        $result['data'] = $data;
        
        $result['type'] = 'http';

        return $result;
    }
    
    public static function sendWebsocketData($data, string $message = '成功') :array
    {
        $result = static::sendData($data, $message);
        
        $result['type'] = 'ws_accpet';
        
        return $result;
    }
    
    public static function sendWebsocketDataSalt($data, string $message = '成功') :array
    {
        return [
            'secretKey' => $data,
            'status' => 1,
            'message' => $message,
            'type' => 'ws_salt'
        ];
    }
    
    public static function sendWebsocketSuccess($data, string $message = '发送成功') :array
    {
        $result =  [
            'status' => 0x006,
            'message' => $message,
            'type' => 'ws_accpet'
        ];
        
        if (!isset($data['cmd'])) {
            $result['data'] = $data;
            
            return $result;
        }
        
        unset($data['cmd']);
        
        $result['data'] = $data;
        
        
        return $result;
    }
    
    public static function sendWebsocketMessage($data, string $message = '成功') :array
    {
        return [
            'data' => [
                'message' => $data,
                'status' => 1,
                'type' => 'message'
            ]
        ];
    }

    public static function sendNativeData(array $data = [], string $message = '成功') :array
    {
        return array_merge([
            'status' => 1,
            'message' => $message
        ], $data);
    }

    /**
     * @param array $data
     * @param string $message
     * @return array
     */
    public static function dispatcher($data, string $message = '信息') :array
    {
        if (!$data) {
            return static::errorNative('操作失败：'.$message);
        }
        return static::sendNativeData($data, $message);
    }


    /**
     * 
     * @param string $message
     * @return array
     */
    public static function error(string $message) :array
    {
        return [
            'data' => '',
            'status' => 0,
            'message' => $message
        ];
    }
    
    /**
     *
     * @param string $message
     * @return array
     */
    public static function errorWebsocket(string $message) :array
    {
        return [
            'data' => '',
            'status' => 0x005,
            'type' => 'message',
            'message' => $message
        ];
    }

    /**
     *
     * @param string $message
     * @return array
     */
    public static function errorNative(string $message) :array
    {
        return array_merge([
            'status' => 0,
            'message' => $message
        ], []);
    }
}